﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;

namespace NLBS.Data
{
    public partial class SqlDataProvider : DbProvider
    {
        public override IList<Widget> GetWidget()
        {
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT * FROM blog_widget order by widget_order");

            IList<Widget> lists = new List<Widget>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Widget widget = CommonDataProvider.PopulateWidgetFromIDataReader(dataReader);

                    lists.Add(widget);
                }
            }
            return lists;
        }

        public override int InsertWidget(Widget widget)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"INSERT INTO [dbo].[blog_Widget] 
            ([widget_Name], [widget_Title], [widget_Hidden], [widget_Type], [widget_Order], [widget_Rate], 
            [widget_InstallTime], [widget_IsInstall], [widget_Content], [widget_IsSystem]) VALUES 
            (@widget_Name, @widget_Title, @widget_Hidden, @widget_Type, @widget_Order, @widget_Rate, 
            @widget_InstallTime, @widget_IsInstall, @widget_Content, @widget_IsSystem)
            SELECT @widgetID=@@IDENTITY");
            db.AddInParameter(dbCommand, "widget_name", DbType.String, widget.Name);
            db.AddInParameter(dbCommand, "widget_Title", DbType.String, widget.Title);
            db.AddInParameter(dbCommand, "widget_Hidden", DbType.Boolean, widget.Hidden);
            db.AddInParameter(dbCommand, "widget_Type", DbType.Int32, widget.Type);
            db.AddInParameter(dbCommand, "widget_Order", DbType.Int32, widget.Order);
            db.AddInParameter(dbCommand, "widget_Rate", DbType.Int32, widget.Rate);
            db.AddInParameter(dbCommand, "widget_InstallTime", DbType.DateTime, widget.InstallTime);
            db.AddInParameter(dbCommand, "widget_IsInstall", DbType.Boolean, widget.IsInstall);
            db.AddInParameter(dbCommand, "widget_Content", DbType.String, widget.Content);
            db.AddInParameter(dbCommand, "widget_IsSystem", DbType.Boolean, false);
            db.AddOutParameter(dbCommand, "widgetID", DbType.Int32, 10);
            db.ExecuteNonQuery(dbCommand);
            int result = Convert.ToInt32(db.GetParameterValue(dbCommand, "@widgetID"));
            return result;
        }

        public override int UpdateWidget(Widget widget)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"UPDATE [dbo].[blog_Widget] SET 
            [widget_Name] = @widget_Name, [widget_Title] = @widget_Title, [widget_Hidden] = @widget_Hidden, 
            [widget_Type] = @widget_Type, [widget_Order] = @widget_Order, [widget_Rate] = @widget_Rate, 
            [widget_InstallTime] = @widget_InstallTime, [widget_IsInstall] = @widget_IsInstall, 
            [widget_Content] = @widget_Content 
            WHERE (([widgetID] = @widgetID))");
            db.AddInParameter(dbCommand, "widget_name", DbType.String, widget.Name);
            db.AddInParameter(dbCommand, "widget_Title", DbType.String, widget.Title);
            db.AddInParameter(dbCommand, "widget_Hidden", DbType.Boolean, widget.Hidden);
            db.AddInParameter(dbCommand, "widget_Type", DbType.Int32, widget.Type);
            db.AddInParameter(dbCommand, "widget_Order", DbType.Int32, widget.Order);
            db.AddInParameter(dbCommand, "widget_Rate", DbType.Int32, widget.Rate);
            db.AddInParameter(dbCommand, "widget_InstallTime", DbType.DateTime, widget.InstallTime);
            db.AddInParameter(dbCommand, "widget_IsInstall", DbType.Boolean, widget.IsInstall);
            db.AddInParameter(dbCommand, "widget_Content", DbType.String, widget.Content);
            
            db.AddInParameter(dbCommand, "widgetID", DbType.Int32, widget.WidgetID);


            return db.ExecuteNonQuery(dbCommand);
        }

        public override int DeleteWidget(int id)
        {
            DbCommand dbCommand = db.GetSqlStringCommand("Delete blog_Widget where widgetID=@widgetID");
            db.AddInParameter(dbCommand, "widgetID", DbType.Int32, id);
           return db.ExecuteNonQuery(dbCommand);
        }

        public override Widget GetWidgetDetail(int id)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT *
            FROM      blog_Widget
            WHERE   (widgetID = @widgetID)");
            db.AddInParameter(dbCommand, "widgetID", DbType.Int32, id);
            IDataReader dr = db.ExecuteReader(dbCommand);
            Widget widget = null;
            if (dr.Read())
            {
                widget = CommonDataProvider.PopulateWidgetFromIDataReader(dr);
            }
            return widget;
        }
    }
}
